package com.wclass.mapper;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.wclass.entity.PostsLike;
import com.wclass.entity.PostsReply;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;

import java.util.List;

/**
 * <p>
 * 帖子回复 Mapper 接口
 * </p>
 *
 * @author SEN
 * @since 2018-12-27
 */
public interface PostsReplyMapper extends BaseMapper<PostsReply> {


    /**
     * <p>
     * 根据 entity 条件，查询全部记录（并翻页）
     * </p>
     *
     * @param rowBounds 分页查询条件（可以为 RowBounds.DEFAULT）
     * @param wrapper   实体对象封装操作类（可以为 null）
     * @return List<T>
     */
    @Override
    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address ${ew.sqlSelect} from posts_reply as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "where info.state=0 and baseAppUser.state=0 ${ew.sqlSegment}")
    List<PostsReply> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<PostsReply> wrapper);

    @Override
    @Select("SELECT COUNT(info.id) from posts_reply info left join posts_info posts on posts.id = info.posts_id where 1=1 ${ew.sqlSegment}")
    Integer selectCount(@Param("ew") Wrapper<PostsReply> wrapper);

    /**
     * 获取未读评论分页
     * @param page
     * @param userId
     * @return
     */
    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address from posts_reply info " +
            "left join base_app_user as baseAppUser on info.user_id = baseAppUser.id " +
            "where info.state = 0 and baseAppUser.state=0 and " +
            "(info.p_id in (select id from posts_reply where user_id = #{userId}) or info.posts_id in (select id from posts_info where user_id = #{userId})) " +
            "order by info.is_new asc,info.crt_time desc")
    List<PostsReply> selectUnreadPostsReplyPage(Pagination page, @Param("userId") Long userId);

    @Select("select count(1) from posts_reply info " +
            "left join base_app_user as baseAppUser on info.user_id = baseAppUser.id " +
            "where info.state = 0 and baseAppUser.state=0 and info.is_new = 0 and " +
            "(info.p_id in (select id from posts_reply where user_id = #{userId}) or info.posts_id in (select id from posts_info where user_id = #{userId})) ")
    Integer selectUnreadPostsReplyCount(@Param("userId") Long userId);
}
